package models

import (
	"fmt"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

// 工单模版结构体
type WorkOrderTemplate struct {
	Model         // 不用每次写ID 和 createAt了
	Name   string `json:"name" validate:"required,min=1,max=50" gorm:"uniqueIndex;type:varchar(100);comment:工单模板名称，支持使用通配符*进行模糊搜索"`
	UserID uint
	// 需要关联2个对象 审批流  表单设计
	FormDesignID uint `json:"formDesignID"`
	ProcessID    uint `json:"processID"`

	Process    *Process    `json:"process" gorm:"-"`
	FormDesign *FormDesign `json:"formDesign" gorm:"-"`

	Key            string `json:"key" gorm:"-"` //给前端表格
	CreateUserName string `json:"createUserName" gorm:"-"`
	ProcessName    string `json:"processName" gorm:"-"`
	FormDesignName string `json:"formDesignName" gorm:"-"`
}

// 根据表单设计ID获取工单模板
func GetWorkOrderTemplateByFormDesignId(formDesignId int) (*WorkOrderTemplate, error) {
	var dbObj WorkOrderTemplate
	err := DB.Where("form_design_id = ?", formDesignId).First(&dbObj).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, fmt.Errorf("WorkOrderTemplate不存在:%v")
		}
		return nil, fmt.Errorf("WorkOrderTemplate查询错误:%v", err)
	}
	return &dbObj, nil

}

// 获取所有工单模版
func GetWorkOrderTemplateAll() (objs []*WorkOrderTemplate, err error) {
	err = DB.Find(&objs).Error
	return
}

// 获取工单模版分页
func GetWorkOrderTemplateByIdsWithLimitOffset(ids []int, limit int, offset int) (objs []*WorkOrderTemplate, err error) {
	err = DB.Limit(limit).Offset(offset).Find(&objs, ids).Error
	return
}

// 创建工单模版
func (obj *WorkOrderTemplate) CreateOneWorkOrderTemplate() error {
	return DB.Create(obj).Error
}

// 填充前端工单模版数据
func (obj *WorkOrderTemplate) FillFrontAllDataOrOrderTemplate() {
	dbUser, _ := GetUserById(int(obj.UserID))
	if dbUser != nil {
		obj.CreateUserName = dbUser.RealName
	}
	dbProcess, _ := GetProcessById(int(obj.ProcessID))
	if dbProcess != nil {
		dbProcess.FillFrontAllData()
		obj.ProcessName = dbProcess.Name
		obj.Process = dbProcess
	}
	dbFormDesign, _ := GetFormDesignById(int(obj.FormDesignID))
	obj.FormDesign = dbFormDesign
	if dbFormDesign != nil {
		obj.FormDesignName = dbFormDesign.Name
	}
	obj.Key = fmt.Sprintf("%d_%s", obj.ID, obj.Name)
}
func (obj *WorkOrderTemplate) FillFrontAllData() {
	dbUser, _ := GetUserById(int(obj.UserID))
	if dbUser != nil {
		obj.CreateUserName = dbUser.RealName
	}

	dbProcess, _ := GetProcessById(int(obj.ProcessID))

	if dbProcess != nil {
		dbProcess.FillFrontAllData()
		obj.ProcessName = dbProcess.Name
		obj.Process = dbProcess
	}

	dbFormDesign, _ := GetFormDesignById(int(obj.ProcessID))
	obj.FormDesign = dbFormDesign
	if dbFormDesign != nil {
		obj.FormDesignName = dbFormDesign.Name
	}

	obj.Key = fmt.Sprintf("%d", obj.ID)

}

// 根据ID获取工单模版
func GetWorkOrderTemplateById(id int) (*WorkOrderTemplate, error) {
	var dbObj WorkOrderTemplate
	err := DB.Where("id = ?", id).First(&dbObj).Error
	return &dbObj, err
}

// 删除工单模版 级联删除 关联数据
func (obj *WorkOrderTemplate) DeleteOneWorkOrderTemplate() error {
	return DB.Select(clause.Associations).Unscoped().Delete(obj).Error
}

// 更新工单模版
func (obj *WorkOrderTemplate) UpdateOneWorkOrderTemplate() error {
	return DB.Where("id = ?", obj.ID).Updates(obj).Error
}
